/*
 * @Author: zhangyang
 * @Date: 2024-05-23 11:41:05
 * @LastEditTime: 2024-06-06 10:10:46
 * @Description: 
 */
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import '/res/colors.dart';
import '/widgets/double_tap_back_exit_app.dart';
import '/providers/home.dart';
import '/pages/tabbar/home.dart';
import '/pages/tabbar/data.dart';

class Home extends StatefulWidget {
  const Home({super.key});

  @override
  State<StatefulWidget> createState() => _HomeState();
}

class _HomeState extends State<Home> with RestorationMixin {
  late List<Widget> _pageList;
  final List<String> _appBarTitles = ['首页', '统计'];
  final PageController _pageController = PageController();

  HomeProvider provider = HomeProvider();

  List<TDBottomTabBarTabConfig>? _list;

  @override
  void initState() {
    super.initState();
    initData();
  }

  @override
  void dispose() {
    _pageController.dispose();
    super.dispose();
  }

  void initData() {
    _pageList = const [
      HomePage(),
      DataPage(),
    ];
  }

  List<TDBottomTabBarTabConfig> _buildBottomNavigationBarItem() {
    if (_list == null) {
      const tabImages = [
        [
          Icon(TDIcons.books),
          Icon(
            TDIcons.books,
            color: Colours.app_main,
          ),
        ],
        [
          Icon(TDIcons.chart_pie),
          Icon(
            TDIcons.chart_pie,
            color: Colours.app_main,
          ),
        ],
      ];
      _list = List.generate(tabImages.length, (i) {
        return TDBottomTabBarTabConfig(
          tabText: _appBarTitles[i],
          unselectedIcon: tabImages[i][0],
          selectedIcon: tabImages[i][1],
          onTap: () {
            _pageController.jumpToPage(i);
          },
        );
      });
    }
    return _list!;
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<HomeProvider>(
      create: (_) => provider,
      child: DoubleTapBackExitApp(
        child: Scaffold(
            bottomNavigationBar: Consumer<HomeProvider>(
              builder: (_, provider, __) {
                return TDBottomTabBar(
                  TDBottomTabBarBasicType.icon,
                  componentType: TDBottomTabBarComponentType.label,
                  outlineType: TDBottomTabBarOutlineType.capsule,
                  useVerticalDivider: true,
                  navigationTabs: _buildBottomNavigationBarItem(),
                );
              },
            ),
            // 使用PageView的原因参看 https://zhuanlan.zhihu.com/p/58582876
            body: PageView(
              physics: const NeverScrollableScrollPhysics(), // 禁止滑动
              controller: _pageController,
              onPageChanged: (int index) => provider.value = index,
              children: _pageList,
            )),
      ),
    );
  }

  @override
  String? get restorationId => 'home';

  @override
  void restoreState(RestorationBucket? oldBucket, bool initialRestore) {
    registerForRestoration(provider, 'BottomNavigationBarCurrentIndex');
  }
}
